package com.theobserver.theobserver;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;


public class SettingActivity extends AppCompatActivity {
    ArrayAdapter adapter;
    private Switch st;
    private boolean isChecked;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        st = (Switch) findViewById(R.id.switch_setting);
        SharedPreferences prefs = getSharedPreferences("tuisong", Context.MODE_PRIVATE);
        isChecked=prefs.getBoolean("service_status",false);
        st.setChecked(isChecked);


        /*AVObject testObject = new AVObject("TestObject");
        testObject.put("words","我是袁舜炜");
        testObject.saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(AVException e) {
                                            if (e == null) {
                                                Log.d("save", "success");
                                            }
                                        }
                                    });*/

        ListView lv = (ListView) findViewById(R.id.ListView2);

        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
                new String[]{"接收推送", "检查更新", "关于应用","用户反馈"});

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long I) {
                //Toast.makeText(SettingActivity.this,
                  //      (String) adapter.getItem(i), Toast.LENGTH_SHORT).show();
                if (i == 2) {
                    startActivity(new Intent(SettingActivity.this, AboutActivity.class));
                }//点击“关于应用”
                else if (i == 1) {
                    startActivity(new Intent(SettingActivity.this,UpdateActivity.class));
                }//点击“检查更新”
                else if (i == 3) {
                    startActivity(new Intent(SettingActivity.this,FeedBackActivity.class));
                }//点击“反馈”
                else {
                //点击“接收推送”
                }
            }
        });



        // 添加监听
        st.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                //Toast.makeText(SettingActivity.this, "已成功恢复", Toast.LENGTH_SHORT).show();

                if (isChecked) {
                    SharedPreferences.Editor editor = getSharedPreferences("tuisong", MODE_PRIVATE).edit();
                    editor.putBoolean("service_status", isChecked);
                    editor.commit();
                    Toast.makeText(SettingActivity.this, "已允许推送", Toast.LENGTH_SHORT).show();
                    //预留接收推送功能
                } else {
                    SharedPreferences.Editor editor = getSharedPreferences("tuisong", MODE_PRIVATE).edit();
                    editor.putBoolean("service_status", isChecked);
                    editor.commit();
                    Toast.makeText(SettingActivity.this, "已禁止推送", Toast.LENGTH_SHORT).show();
                }

            }
        });
        TextView tv = (TextView) findViewById(R.id.textView_setting_return);//返回按钮
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingActivity.this.finish();//预留
            }
        });
    }
}

